import java.util.Scanner;

public class PokerDemo {
    public static void main(String[] args) {
        PokerManager manager = new PokerManager();
        Scanner scanner = new Scanner(System.in);
        boolean start = true;
        while (start){
            showMenu.gameHallMenu();
            System.out.print("请选择游戏类型：");
            int sel = scanner.nextInt();
            switch (sel){
                case 1:
                    start = false;
                    break;
                case 2:
                case 3:
                    System.out.println("正在升级功能，敬请期待！");
                    break;
                default:
                    System.out.println("输入有误请重新输入");
                    break;
            }
        }
        showMenu.roomMenu();
        System.out.print("请选择桌号：");
        int sel;
        while (true) {
            sel = scanner.nextInt();
            if (sel >= 0 && sel <= 3) {
                break;
            } else
                System.out.print("输入有误,请重新输入:");
        }
        //创建一个四人小桌
        game1Table game1 = new game1Table(sel);
        for(int i = 0;i<4;i++){
            //扣除底注费
            game1.players.get(i).subMoney(game1.ante);
        }
        //显示加入房间的人
        for(int i=0;i<4;i++){
            System.out.println(game1.players.get(i).name+"加入房间...");
        }
        //发牌员为每人发一张牌
        manager.dealPoker(game1.players);
        int choice = 0;
        boolean begain = true;
        while (begain){

            int i = 0;
            int flag = 0;
            for(i=0;i<4;i++){
                //如果当前玩家状态为FALSE则退出本次循环
                if(game1.players.get(i).status == false)
                    continue;
                //显示桌面玩家信息
                game1.showPlayers();
                System.out.println("当前玩家"+"\t"+(i+1)+"号"+game1.players.get(i).name);

                //如果一个人没有选择All in并且余额大于底注或上一个人的下注输出5选项菜单否则输出2选项菜单
                if(choice != 4&&game1.players.get(i).balance>game1.minBet){
                    showMenu.selectMenu();
                }else{
                    showMenu.selectMenuAll_In();
                    flag = -1;
                }
                boolean tag1 = true;
                while (tag1){
                    System.out.print("请选择操作：");
                    choice = scanner.nextInt();
                    switch (choice){
                        //下注
                        case 1:
                            if(flag == -1){
                                System.out.println("输入有误！请输入数字3或4");
                                break;
                            }
                            boolean tag = true;
                            while (tag){
                                System.out.print("请输入下注金额：");
                                int ante = scanner.nextInt();
                                if(game1.players.get(i).Bet(ante)){
                                    System.out.println("下注成功！");
                                    //存储当前玩家下注值
                                    game1.minBet = ante;
                                    //结束tag循环
                                    tag = false;
                                }else{
                                    System.out.println("下注失败！请重新输入");
                                }
                            }
                            //结束tag1循环
                            tag1 = false;
                            break;
                        //跟注
                        case 2:
                            if(flag == -1){
                                System.out.println("输入有误！请输入数字3或4");
                                break;
                            }
                            //跟注上一个玩家下注值
                            if(game1.players.get(i).Bet(game1.minBet)){
                                //结束tag1循环
                                tag1 = false;
                                break;
                            }else {
                                System.out.println("余额不足跟注失败！");
                                //继续tag1循环
                                break;
                            }
                            //弃牌
                        case 3:
                            //结束tag1循环
                            tag1 = false;
                            //玩家设置false状态
                            game1.players.get(i).status = false;
                            break;
                        //all in
                        case 4:
                            //结束tag1循环
                            tag1 = false;

                            int money = game1.players.get(i).all_In();
                            //如果all in金额大于最小赌注，更新最小赌注
                            if(money>game1.minBet){
                                game1.minBet = money;
                            }

                            //判断自己的牌是不是四张牌最小的，如果是，将自己设为状态FALSE
                            for (Player player: game1.players
                            ) {
                                if(player!=game1.players.get(i)){
                                    if (!game1.players.get(i).compareCard(player)) {
                                        game1.players.get(i).status = false;
                                    }
                                }
                            }
                            break;
                        //比牌
                        case 5:
                            if(flag == -1){
                                System.out.println("输入有误！请输入数字3或4");
                                break;
                            }
                            boolean tag2 = true;
                            while (tag2){
                                System.out.print("请输入要比的玩家编号：");
                                int t = scanner.nextInt();
                                if(t>=1&&t<=4){
                                    //如果玩家一点数比玩家二大那么玩家一将获得玩家二之前下的赌注之和
                                    if(game1.players.get(i).compareCard(game1.players.get(t-1))){
                                        game1.players.get(i).addMoney(game1.players.get(t-1).betMoney);
                                        game1.players.get(t-1).subMoney(game1.players.get(i).betMoney);
                                        game1.players.get(t-1).status = false;
                                        System.out.println("玩家"+game1.players.get(i).name+"比牌胜利");
                                    }else {
                                        game1.players.get(t-1).addMoney(game1.players.get(i).betMoney);
                                        game1.players.get(i).subMoney(game1.players.get(t-1).betMoney);
                                        game1.players.get(i).status = false;
                                        System.out.println("玩家"+game1.players.get(t-1).name+"比牌胜利");
                                    }
                                    //跳出tag2循环
                                    tag2 = false;
                                    tag1 = false;
                                }else
                                    System.out.println("您输入的玩家编号有误请重新输入！");
                            }
                            break;
                        default:
                            System.out.println("您输入的有误！请重新输入");
                            break;
                    }
                }
                int temp = 0;
                //判断桌面玩家状态是否为TRUE，不是的话将其算入temp中
                for(int t = 0;t<4;t++){

                    if(game1.players.get(t).status!=true)
                        temp++;
                }
                //如果桌面玩家FALSE状态大于等于3则游戏结束
                if(temp>=3){
                    System.out.println("\t游戏结束\t");
                    //找到最小All in金额
                    for (Player player :
                            game1.players) {
                        if(player.all_inMoney<game1.minBet){
                            game1.minBet = player.all_inMoney;
                        }
                    }
                    int tam = 0;
                    for(int t = 0;t<4;t++){
                        if(game1.players.get(t).status==true){
                            tam = t;
                            for(int p =0;p<4;p++){
                                game1.players.get(t).addMoney(game1.players.get(p).all_inMoney);
                            }
                            System.out.println("恭喜玩家"+game1.players.get(t).name+"赢得比赛!");
                        }
                    }
                    for(int t = 0;t<4;t++){
                        //如果all in 的时候金额大于all in金额最小的玩家则返现多掏的钱
                        if(game1.players.get(t).all_inMoney>game1.minBet){
                            int ret = game1.players.get(t).all_inMoney-game1.minBet;
                            game1.players.get(t).addMoney(ret);
                            if(t!=tam){
                                game1.players.get(tam).subMoney(ret);
                            }
                        }
                    }

                    //游戏结束展示玩家信息
                    game1.showPlayers();
                    begain = false;
                    break;
                }
            }

        }

    }
}
